package com.ps.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ps.pojo.Premission;
import com.ps.pojo.Role;
import com.ps.service.PremissionService;
import com.ps.service.RoleService;
import com.ps.utils.Result;
import com.ps.vo.TreeNode;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-12-18
 */
@Controller
@RequestMapping("/xt/jsgl")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private PremissionService premissionService;
    @GetMapping("/getAll")
    @ResponseBody
    public Result getAll(){
        List<Role> list=roleService.list();
        return Result.success(list);
    }
    @PostMapping("/del")
    @ResponseBody
    public Result delById(Integer id){
        boolean flag=roleService.removeById(id);
        Result result=null;
        if (flag){
            result=result.success("删除成功！");
        }else {
            result=result.fail("删除失败！");
        }
        return result;
    }
    @PostMapping("/add")
    @ResponseBody
    public Result addRole(String rolename,String rolecode){
        Role role=new Role();
        role.setRolecode(rolecode);
        role.setRolename(rolename);
        role.setCreationtime(LocalDateTime.now());
        role.setCreatedby("创建者");
        role.setIsstart(1);
        boolean flag=roleService.add(role);
        Result result=null;
        if (flag){
            result=result.success("添加成功！");

        }else {
            result=result.fail("添加失败！");
        }
        return result;
    }
    @PostMapping("/update")
    @ResponseBody
    public Result updateRole(Role role){
        role.setLastupdatetime(LocalDateTime.now());
        boolean flag=roleService.updateById(role);
        Result result=null;
        if (flag){
            result=result.success("修改成功！");

        }else {
            result=result.fail("修改失败！");
        }
        return result;
    }
    @PostMapping("/tree")
    @ResponseBody
    public Result tree(Integer roleid){
        List<TreeNode> treeData = roleService.getTreeData(roleid);
        return Result.success(treeData);
    }
    @PostMapping("/grant")
    @ResponseBody
    public Result grant(Integer roleid,Integer[]ids){
        Result result=new Result();
        if (roleService.grant(roleid,ids)){
            result.setMsg("授权成功！");
            return result;
        }
        result.setMsg("授权失败！");
        return result;
    }
}

